package sys.spvisor.console.controller;

import java.io.IOException;
import java.io.InputStream;  
import java.io.PrintWriter;  
import java.util.HashMap;
import java.util.List;  
import java.util.Map;
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.multipart.MultipartFile;  
import org.springframework.web.multipart.MultipartHttpServletRequest;  

import sys.spvisor.core.service.project.UploadExcelService;
 
  
@Controller  
@RequestMapping("/uploadExcel")    
public class UploadExcelControl {  
	 private static Log log = LogFactory.getLog(UploadExcelControl.class);
	@Autowired
    private UploadExcelService customerService;
    
    @RequestMapping(value = "/batchimport.ajax", method = RequestMethod.POST)
    public Map<String,Object> batchimport(@RequestParam(value="filename") MultipartFile file,
            HttpServletRequest request,HttpServletResponse response) throws IOException{
        log.info("UploadExcelControl ..batchimport() start");
        Map<String,Object> result = new HashMap<String, Object>();
        //判断文件是否为空
        if(file==null) return null;
        //获取文件名
        String name=file.getOriginalFilename();
        //进一步判断文件是否为空（即判断其大小是否为0或其名称是否为null）
        long size=file.getSize();
        if(name==null || ("").equals(name) && size==0) return null;
        
        //批量导入。参数：文件名，文件。
        boolean b = customerService.batchImport(name,file);
        if(b){
             String Msg ="批量导入EXCEL成功！";
             result.put("message", Msg);
        }else{
             String Msg ="批量导入EXCEL失败！";
             result.put("message", Msg);
        } 
       return result;
    }
  
}  
